/*
 * @lc app=leetcode.cn id=1022 lang=javascript
 *
 * [1022] 从根到叶的二进制数之和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumRootToLeaf = function(root) {
    let sum=0
    let arr=[]
    let def=function(node,str){
        if(node.left){
            def(node.left,str+node.val)
        }
        if(node.right){
            def(node.right,str+node.val)
        }
        if(!node.left&&!node.right){
            str=str+node.val
            arr.push(str)
        }
    }
    def(root,"")
    for(let item of arr){
        sum+=parseInt(item,2)
    }
    return sum
};
// @lc code=end

